﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using LRSTaxServices.DAL;
using LRSTaxServices.DAL.dsTaxServicesSchedularsTableAdapters;
using LRSTaxServices.Common;

namespace LRSTaxServices.Biz
{
    /// <summary>
    /// TaxServicesSchedulars Business Object
    /// </summary>

    public class TaxServicesSchedular : BOBase
    {


        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected TaxServicesSchedular() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the TaxServicesSchedulars.
        /// </summary>
        public int ScheduleID
        {
            get { return _scheduleid; }
            set
            {
                if (_scheduleid != value)
                {
                    if (OnPropertyChanging("ScheduleID", _scheduleid, value))
                    {
                        int oldValue = _scheduleid;
                        _scheduleid = value;

                        OnPropertyChanged("ScheduleID", oldValue, value);
                    }
                }
            }
        }  private int _scheduleid = -1;

        /// <summary>
        /// FullName .
        /// </summary>
        public string FullName
        {
            get { return _fullname; }
            set
            {
                if (_fullname != value)
                {
                    if (OnPropertyChanging("FullName", _fullname, value))
                    {
                        string oldValue = _fullname;
                        _fullname = value;
                        OnPropertyChanged("FullName", oldValue, value);
                    }
                }
            }
        }  private string _fullname = null;


        /// <summary>
        /// ScheduleDate.
        /// </summary>
        public string ScheduleDate
        {
            get { return _scheduledate; }
            set
            {
                if (_scheduledate != value)
                {
                    if (OnPropertyChanging("ScheduleDate", _scheduledate, value))
                    {
                        string oldValue = _scheduledate;
                        _scheduledate = value;
                        OnPropertyChanged("ScheduleDate", oldValue, value);
                    }
                }
            }
        }  private string _scheduledate = null;


        /// <summary>
        /// Schedule Time.
        /// </summary>
        public string ScheduleTime
        {
            get { return _scheduletime; }
            set
            {
                if (_scheduletime != value)
                {
                    if (OnPropertyChanging("ScheduleTime", _scheduletime, value))
                    {
                        string oldValue = _scheduletime;
                        _scheduletime = value;
                        OnPropertyChanged("ScheduleTime", oldValue, value);
                    }
                }
            }
        }  private string _scheduletime = null;



        /// <summary>
        /// Phone.
        /// </summary>
        public string Phone
        {
            get { return _phone; }
            set
            {
                if (_phone != value)
                {
                    if (OnPropertyChanging("Phone", _phone, value))
                    {
                        string oldValue = _phone;
                        _phone = value;
                        OnPropertyChanged("Phone", oldValue, value);
                    }
                }
            }
        }  private string _phone = null;



        /// <summary>
        /// Date/time at which the Schedule was added.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static TaxServicesSchedular CreateNew()
        {
            TaxServicesSchedular obj = new TaxServicesSchedular();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now; ;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static TaxServicesSchedular CreateNew(TaxServicesSchedularCollection collection)
        {
            TaxServicesSchedular obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (TaxServicesSchedular taxservicesschedular in collection)
            {
                if (taxservicesschedular.ScheduleID < minVal)
                    minVal = taxservicesschedular.ScheduleID;
            }
            obj.ScheduleID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllTaxServicesSchedulars

        /// <summary>
        /// Returns a collection of all TaxServicesSchedulars.
        /// </summary>
        public static TaxServicesSchedularCollection GetAllTaxServicesSchedulars()
        {
            TaxServicesSchedularsTableAdapter adapter = new TaxServicesSchedularsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsTaxServicesSchedulars.TaxServicesSchedularsDataTable tbl = adapter.GetData(null);

            TaxServicesSchedularCollection list = new TaxServicesSchedularCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsTaxServicesSchedulars.TaxServicesSchedularsRow row in tbl.Rows)
                {
                    TaxServicesSchedular taxservicesschedular = new TaxServicesSchedular();
                    RelationalToObject(taxservicesschedular, row);
                    list.Add(taxservicesschedular);
                }
            }

            return (list);
        }

        #endregion

        #region GetTaxServicesSchedularByID

        /// <summary>
        /// Returns a TaxServicesSchedular object for the specified 'TaxServicesSchedularsID' or null if no TaxServicesSchedulars with the specified parameter(s) exists.
        /// </summary>
        public static TaxServicesSchedular GetTaxServicesSchedularByID(int schedularid)
        {
            TaxServicesSchedularsTableAdapter adapter = new TaxServicesSchedularsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsTaxServicesSchedulars.TaxServicesSchedularsDataTable tbl = adapter.GetData(schedularid);

            if (tbl.Rows.Count == 1)
            {
                TaxServicesSchedular TaxServicesSchedular = new TaxServicesSchedular();
                RelationalToObject(TaxServicesSchedular, tbl.Rows[0]);
                return (TaxServicesSchedular);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'TaxServicesSchedular' returned more than one TaxServicesSchedular record.");

            return (null);
        }

        #endregion        

        #region Update

        /// <summary>
        /// Updates the specified TaxServicesSchedular.
        /// </summary>
        public static void Update(TaxServicesSchedular obj)
        {
            bool updateTaxServicesSchedular = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateTaxServicesSchedular = (obj.ObjState != ObjectState.Unchanged);

                if (!updateTaxServicesSchedular)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsTaxServicesSchedulars.TaxServicesSchedularsDataTable tbl = null;
            if (updateTaxServicesSchedular)
            {
                TaxServicesSchedularsTableAdapter adapter = new TaxServicesSchedularsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsTaxServicesSchedulars.TaxServicesSchedularsDataTable();
                dsTaxServicesSchedulars.TaxServicesSchedularsRow row = tbl.NewTaxServicesSchedularsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (ScheduleID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("TaxServicesSchedularID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((FullName == null) || (FullName.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("FullName", "FullName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("TaxServicesSchedular: ScheduleID={0}, FullName={1}", ScheduleID, FullName));
        }

        #endregion

        #endregion       

    }

    /// <summary>
    /// Collection of 0 or more TaxServicesSchedulars objects.
    /// </summary>

    public class TaxServicesSchedularCollection : List<TaxServicesSchedular>
    {
        #region Types

        public class UserComparer : IComparer<TaxServicesSchedular>
        {
            public int Compare(TaxServicesSchedular r1, TaxServicesSchedular r2)
            {
                return (String.Compare(r1.ScheduleID.ToString(), r2.ScheduleID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindBySchedularID

        public TaxServicesSchedular FindBySchedularID(int schedularid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ScheduleID == schedularid)
                    return (this[ndx]);

            return (null);
        }

        #endregion        

        #endregion
    }
}
